<template>
  <div class="goods">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'/home/goods'}">商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <!-- 头部搜索添加 -->
      <el-row class="userSearch">
        <el-col :span='8' class="search">
          <el-input placeholder="请输入内容" v-model.trim="input" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
          </el-input>
        </el-col>
        <el-col :span='4'>
          <el-button type="primary" @click="dialogFormVisible = true">
            添加用户
          </el-button>
        </el-col>
      </el-row>
      <!--对话框 from表单添加用户 -->
      <el-table :data="goodsList" border stripe style="width: 100%" :header-cell-style="{textAlign: 'center'}" :cell-style="{ textAlign: 'center' }">
        <el-table-column type="index" label="#" width="80"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="500"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="160"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="light" content=" 编辑" placement="top-start" :enterable='false'>
              <el-button type="primary" icon="el-icon-edit-outline" size='mini'></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="light" content=" 删除" placement="top-start" :enterable='false'>
              <el-button type="danger" icon="el-icon-delete" size='mini' @click="deleteGoods(scope.row.goods_id)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="light" content=" 设置" placement="top-start" :enterable='false'>
              <el-button type="warning" icon="el-icon-setting" size='mini'></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[3, 5, 10, 15]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>
   <!-- 作用域插槽 slot-scope="scope"-->
<script>
export default {
  name: 'Good',
  data() {
    return {
      input: '',
      goodsList: [],
      queryInfo: {
        pagenum: 1,
        pagesize: 5,
        query: ''
      },
      total: 0
    }
  },
  methods: {
    // 获取googs数据
    async getGoodsList() {
      const { data } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      // 改变时间格式
      data.data.goods.forEach(item => {
        let date = new Date(item.add_time)
        item.add_time = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
      })
      this.goodsList = data.data.goods
      this.total = data.data.total
      console.log(data)
    },
    // 搜索
    search() {},
    // 添加
    dialogFormVisible() {},
    handleCurrentChange(num) {
      this.queryInfo.pagenum = num
      this.getGoodsList()
    },
    handleSizeChange(size) {
      this.queryInfo.pagesize = size
      this.getGoodsList()
    },
    async deleteGoods(id) {
      const { data } = await this.$http.delete(`goods/${id}`)
      console.log(data)
      if (data.meta.status == 200) return this.$message.success('删除成功')
    }
  },
  created() {
    this.getGoodsList()
  }
}
</script>